package com.xuetang9.t9.hotel.controller;

import com.xuetang9.t9.hotel.beans.EntryFood;
import com.xuetang9.t9.hotel.beans.EntryOrder;
import com.xuetang9.t9.hotel.dto.reqest.EntryOrderDTO;
import com.xuetang9.t9.hotel.services.IEntryOrderService;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Classname EntryFoodController
 * @Description TODO
 * @Date 2020/2/24 16:36
 * @Created by ASUS
 */
@RestController
@RequestMapping("entry")
public class EntryFoodController {
    @Autowired
    private IEntryOrderService entryOrderService;

    @GetMapping("list")
    public List<EntryOrder> list(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = dateFormat.format(new Date());
//        Date nowDate = null;
//        try {
//            nowDate = dateFormat.parse("2020-01-07");
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        entryOrderService.queryByNowDate(nowDate).forEach(System.out::println);
        return entryOrderService.queryByNowDate(nowDate);
    }

    @PostMapping("add")
    public boolean add(@RequestBody EntryOrderDTO dto){
        System.out.println(dto);
        Date now = new Date();
        EntryOrder entryOrder = new EntryOrder(now,now,dto.getObligation(),dto.getRemark(),dto.getEntryFoods());
        System.out.println(entryOrder);
        return entryOrderService.addEntry(entryOrder);
    }

    @GetMapping("bill/{id}")
    public EntryOrder billOrder(@PathVariable(value = "id",required = true)Long id){
        EntryOrder entryOrder = entryOrderService.toUp(id);
        entryOrderService.delEntry(id);
        return entryOrder;
    }

    @GetMapping("del")
//    @ResponseBody//java对象转为json
    public boolean removeOrder(@RequestParam("id") Long id){
        return entryOrderService.delEntry(id);
    }
}
